package cn.cau.service.impl;
import cn.cau.dao.CodeNumberDao;
import cn.cau.entity.CodeNumber;
import cn.cau.entity.CodeRule;
import cn.cau.service.CodeNumberService;
import com.google.zxing.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.text.NumberFormat;
@Service
public class CodeNumberServiceImpl implements CodeNumberService {
    @Autowired
    private CodeNumberDao codeSearchDao;
    @Override
    public List<CodeNumber> getAllRecoreds(String codeNumber) {
        return codeSearchDao.findAllRecords(codeNumber);
    }

    @Override
    public List<CodeNumber> getAllRecored(int id) {
        return codeSearchDao.findAllRecord(id);
    }
    @Override
    public String getCategoryName(int categoryId)
    {
        String a=codeSearchDao.getCategoryName(categoryId);
        return codeSearchDao.getCategoryName(categoryId);

    }
    @Override
    public String getRuleCategoryName(int ruleCategoryId)
    {
        return codeSearchDao.getRuleCategoryName(ruleCategoryId);
    }

    @Override
    public String codeGenerate(int serial,
                               CodeRule coderule,
                               int coderuleid,
                               int categoryid,
                               int rulecategoryid,
                               List<String> categoryOptions,
                               List<String> categoryOptionsLength)
    {
        String codenumber;
        String Rulecategoryid=Integer.toString(rulecategoryid);
        String Categoryid="";
        String Codecode="";
        String Serial="";
        String Serial_temp="";
        //if(categoryid<10) Categoryid=String.format("%02d", categoryid);
        //       else Categoryid=Integer.toString(categoryid);
        Categoryid=codeSearchDao.getCategory(categoryid);
        for (int i = 0; i <categoryOptions.size(); i++) {
            if(categoryOptions.get(i).length()==categoryOptionsLength.get(i).length()||categoryOptions.get(i).length()==Integer.valueOf(categoryOptionsLength.get(i)))
            {
                Codecode+=categoryOptions.get(i);
            }
            else
            {
                int a=Integer.valueOf(categoryOptionsLength.get(i));
                int b=Integer.valueOf(categoryOptions.get(i));
                NumberFormat nff = NumberFormat.getInstance();
                nff.setGroupingUsed(false);
                nff.setMaximumIntegerDigits(a);
                nff.setMinimumIntegerDigits(a);
                Codecode+=nff.format(b);
            }

        }
        int count=codeSearchDao.findNum(coderuleid);
        count=count+1;
        Serial_temp=Integer.toString(count);
        NumberFormat nf = NumberFormat.getInstance();
        nf.setGroupingUsed(false);
        nf.setMaximumIntegerDigits(serial);
        nf.setMinimumIntegerDigits(serial);
        Serial=nf.format(count);

        codenumber=Rulecategoryid+Categoryid+Codecode+Serial;

        CodeNumber co=new CodeNumber();
        co.setCodeNumber(codenumber);
        co.setCategoryId(categoryid);
        co.setRuleCategoryId(rulecategoryid);
        co.setCodeRuleId(coderuleid);
        co.setCodeNumberId(0);
        codeSearchDao.insertCodeNumber(co);
        return codenumber;
    }


}
